<template>
  <div class="update-birthday">
    <van-datetime-picker
      v-model="currentDate"
      type="date"
      title="选择年月日"
      :min-date="minDate"
      :max-date="maxDate"
      @cancel="$emit('close')"
      @confirm="onConFirm"
    />
  </div>
</template>

<script>
import { updateUserProfile } from '@/api/user'
import dayjs from 'dayjs'
export default {
  name: 'UpdateBirthday',
  components: {},
  props: {
    value: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      minDate: new Date(1970, 0, 1),
      maxDate: new Date(),
      currentDate: new Date(this.value)
    }
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    async onConFirm() {
      this.$toast.loading({
        message: '修改中，请稍后... ...',
        forbidClick: true,
        duration: 0
      })
      try {
        const currentDate = dayjs(this.currentDate).format('YYYY-MM-DD')
        await updateUserProfile({ birthday: currentDate })
        // 更新视图
        this.$emit('input', currentDate)
        // 关闭弹窗
        this.$emit('close')
        // 提示成功
        this.$toast('修改成功！')
      } catch (error) {
        this.$toast('更新失败，请稍后重试！')
      }
    }
  }
}
</script>

<style scoped lang="less"></style>
